﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace MyDrive
{
    class Program
    {
        static void Main(string[] args)
        {
            String path1 = @"C:\data\temp\mydrive\cv1.doc";
            String path2 = @"C:\data\temp\mydrive\cv2.doc";

            FileStream file1 = File.OpenRead(path1);
            FileStream file2 = File.OpenRead(path2);

            var md5 = MD5.Create();            
           
            //Console.WriteLine( BitConverter.ToString(md5.ComputeHash(file1)).Replace("-", "").ToLower()) ;
            //Console.WriteLine( BitConverter.ToString(md5.ComputeHash(file2)).Replace("-", "").ToLower()) ;
            
            long len1 = file1.Length ;
            long len2 = file2.Length ;

            byte[] arrBytes1 = new byte[len1];
            byte[] arrBytes2 = new byte[len2];

            int PACKET_SIZE =512 ;

            file1.Read(arrBytes1, 0, (int)len1);
            file2.Read(arrBytes2, 0, (int)len2);

            file1.Close();
            file2.Close();

            long maxi = len1;
            if (len2 < len1) { maxi = len2; }

            long nb = 0;

            long nbPaquets = (long) maxi/PACKET_SIZE;

            for (long i = 0; i < nbPaquets; i++)
            {
                bool ok = true;
                byte[] paquet1 = new byte[PACKET_SIZE];
                byte[] paquet2 = new byte[PACKET_SIZE];

                Array.Copy(arrBytes1, i * PACKET_SIZE, paquet1, 0, PACKET_SIZE);
                Array.Copy(arrBytes2, i * PACKET_SIZE, paquet2, 0, PACKET_SIZE);
               

                //comparer les MD5
                String check1 = BitConverter.ToString(md5.ComputeHash(paquet1)).Replace("-", "").ToLower();
                String check2 = BitConverter.ToString(md5.ComputeHash(paquet2)).Replace("-", "").ToLower();

                if (check1.Equals(check2))
                {
                    Console.WriteLine(check1);
                    
                }
                else
                {
                    ok = false;
                }
               if (ok) { nb++; }
            }

            Console.WriteLine("nb : " + nb + "/" + nbPaquets + ":" +  (100*nb/nbPaquets) + "%" ) ;
            Console.ReadKey();
        }
    }
}
